package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;


import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ScreenUtils;
import reduce.fz.com.reduce.util.http.ImageLoader;

/**
 * 流里面点赞/投票用户列表的adapter
 */
public class CardLikeUserListAdapter extends RecyclerView.Adapter<CardLikeUserListAdapter.UserViewHolder> {
    private Context mContext;
    /**
     * 点赞用户列表
     */
    private List<User> mUserList = new ArrayList<User>();
    /**
     * 图片展示大小根据屏幕宽度而定
     */
    private final int imageWidth;
    private int maxNum;

    private OnClickUserAvatarListener mOnClickUserAvatarListener;
    private ImageLoader mImageLoader = HttpUtils.getAvatarImageLoader();

    public CardLikeUserListAdapter(Context context, OnClickUserAvatarListener onClickUserAvatarListener) {
        this.mContext = context;
        mOnClickUserAvatarListener = onClickUserAvatarListener;

        int pl = context.getResources().getDimensionPixelSize(R.dimen.dimen_150);
        //屏幕宽度+
        int sw = ScreenUtils.getScreenW();
        sw = sw - pl;
        int initW = context.getResources().getDimensionPixelSize(R.dimen.card_user_like_list_avatar_size);
        int p = context.getResources().getDimensionPixelSize(R.dimen.card_user_like_list_avatar_padding) * 2;
        maxNum = sw / (p + initW);
        imageWidth = (sw - maxNum * (p + initW)) / maxNum + initW;
        maxNum--;
    }

    /**
     * 设置最大数
     * @param _maxNum
     */
    public void setMaxNum(int _maxNum){
        maxNum = _maxNum;
    }
    /**
     * 删除用户头像
     *
     * @param user
     */
    public void removeUser(User user) {
        for (int i = 0, len = mUserList.size(); i < len; i++) {
            User u = mUserList.get(i);
            if (u.uid != null && u.uid.equals(user.uid)) {
                mUserList.remove(i);
                notifyItemRemoved(i);
                break;
            }
        }
    }

    /**
     * 新增头像在最前面
     *
     * @param user
     */
    public void prependUser(User user) {
        mUserList.add(0, user);
        notifyItemInserted(0);
    }

    /**
     * 替换user列表
     *
     * @param userList
     */
    public void replaceLikeUsers(List<User> userList) {
        mUserList.clear();
        if (userList != null) {
            int num = Math.min(userList.size(), maxNum);
            for (int i = 0; i < num; i++) {
                mUserList.add(userList.get(i));
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_like_user_avatar, parent, false);
        return new UserViewHolder(v, imageWidth, this);
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, final int position) {
        User user = mUserList.get(position);
        holder.renderViewHolder(user, this);
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mUserList.size();
    }

    /**
     * UserViewHolder
     */
    public static class UserViewHolder extends RecyclerView.ViewHolder {
        public RoundNetworkImageView mAvatarView;
        //        public ImageView mGenderView;
        public User mUser;

        public UserViewHolder(View v, int imageWidth, final CardLikeUserListAdapter adapter) {
            super(v);
            mAvatarView = (RoundNetworkImageView) v.findViewById(R.id.user_avatar);
//            mGenderView = (ImageView) v.findViewById(R.id.user_gender);

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(imageWidth, imageWidth);
            mAvatarView.setLayoutParams(layoutParams);

            mAvatarView.setDefaultImageResId(R.drawable.avatar_default);
            mAvatarView.setErrorImageResId(R.drawable.avatar_default);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mUser != null)
                        adapter.mOnClickUserAvatarListener.onClickUser(mUser);
                }
            });
        }

        public void renderViewHolder(User user, final CardLikeUserListAdapter adapter) {
            mUser = user;
            if (user == null) {
                return;
            }

            mAvatarView.setImageUrl(user.avatar, adapter.mImageLoader);

        }
    }

    public interface OnClickUserAvatarListener {
        public void onClickUser(User user);
    }
}
